#define MAXSIZE 20
#include<stdio.h>
typedef struct
{
    char str[MAXSIZE];
    int length;
}SeqString;

void StrAssign(SeqString *S, char cstr[])
{
    int i = 0;
    for(i = 0; cstr[i] != '\0'; i++)
    {
        S->str[i] = cstr[i];
    }
    S->length = i;
}

int StrEmpty(SeqString *S)
{
    if(S->length == 0) return 1;
    else return 0;
}

int StrLength(SeqString *S)
{
    return S->length;
}

void StrCopy(SeqString *T, SeqString *S)
{
    for(int i = 0;i < S->length ; i++)
    {
        T->str[i] = S->str[i];
    }
    T->length = S->length;
}

int StrCompare(SeqString *S, SeqString *T)
{
    int i;
    for(i = 0; i < S->length && i < T->length; i++)
    {
        if(S->str[i] != T->str[i])
        {
            return S->length - T->length;
        }
    }
    return S->length - T->length;
}

int StrInsert(SeqString *S, int pos, SeqString *T)
{
    if(pos < 0 || pos-1 > S->length)
    {
        printf("插入位置不正确\n");
        return 0;
    }   
    if(S->length + T->length <= MAXSIZE)
    {
        for(int i = S->length + T->length - 1; i >= pos + T->length - 1; i--)
        {
            S->str[i] = S->str[i - T->length];
        }
        for(int i = 0; i < T->length; i++)
        {
            S->str[i + pos] = T->str[i];
        }
        S->length = S->length + T->length;
        return 1;
    }
    else
    if(pos + T->length <= MAXSIZE)
    {
        for(int i = MAXSIZE-1; i >= pos + T->length - 1; i--)
        {
            S->str[i] = S->str[i - T->length];
        }
        for(int i = 0; i < T->length; i++)
        {
            S->str[i+pos] = T->str[i];
        }
        S->length = MAXSIZE;
        return 0;
    }
    else
    {
        for(int i = 0; i < MAXSIZE - pos; i++)
        {
            S->str[i+pos] = T->str[i];
        }
        S->length = MAXSIZE;
        return 0;
    }
}

int StrDelete(SeqString *S, int pos, int len)
{
    if(pos <= 0 || len < 0 || pos+len-1 >= S->length)
    {
        printf("参数不合法");
        return 0;
    }
    else
    {
        for(int i = pos + len - 1; i < S->length-1; i++)
        {
            S->str[i-len] = S->str[i];
        }
        S->length = S->length - len;
        return 1;
    }
}

int StrConcat(SeqString *T,SeqString *S)
{
    if(T->length + S->length <= MAXSIZE)
    {
        for(int i = T->length; i < T->length + S->length; i++)
        {
            T->str[i] = S->str[i-T->length];
        }
        T->length = T->length + S->length;
        return 1;
    }
    else
    {
        for(int i = T->length; i < MAXSIZE; i++)
        {
            T->str[i] = S->str[i-T->length];
        }
        T->length = MAXSIZE;
        return 0;
    }
}

void ClearStr(SeqString *S)
{
    S->length = 0;
}

void PrintStr(SeqString *S)
{
    for(int i = 0; i< S->length; i++)
    {
        printf("%c",S->str[i]);
    }
    printf("\n");
}